﻿using AiBLSmartEdu.Module.AIMedicalAssistant.API.DTOs;
using AiBLSmartEdu.Module.AIMedicalAssistant.API.Services;
using FrameworkCore;
using FrameworkCore.Controllers;
using FrameworkCore.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace AiBLSmartEdu.Module.AIMedicalAssistant.API.Controllers;

/// <summary>
/// AI+就医助手
/// </summary>
[AllowAnonymous]
public class MedicalAssistantController : BaseModuleController
{
    private readonly KnowledgeBaseService _knowledgeBaseService;
    private readonly DialogueService _dialogueService;
    private readonly MedicalAssistantService _medicalAssistantService;

    public MedicalAssistantController(
        KnowledgeBaseService knowledgeBaseService,
        DialogueService dialogueService,
        MedicalAssistantService medicalAssistantService)
    {
        _knowledgeBaseService = knowledgeBaseService;
        _dialogueService = dialogueService;
        _medicalAssistantService = medicalAssistantService;
    }

    /// <summary>
    /// 创建知识库
    /// </summary>
    /// <param name="model">输入模型</param>
    /// <returns></returns>
    [HttpPost("Create/KnowledgeBase")]
    public async Task<Result> CreateAsync(KnowledgeBaseCreateInputModel model)
    {
        await _knowledgeBaseService.CreateAsync(model);
        return await Result.SucceedAsync();
    }

    /// <summary>
    /// 知识库分页查询
    /// </summary>
    /// <param name="filter">分页输入模型</param>
    /// <returns></returns>
    [HttpGet("KnowledgeBase/Page")]
    public async Task<PageResult<KnowledgeBaseOutputModel>> PaginationAsync([FromQuery] KnowledgeBasePaginationFilter filter)
    {
        return await _knowledgeBaseService.PageAsync(filter);
    }

    /// <summary>
    /// 删除知识库
    /// </summary>
    /// <returns></returns>
    [HttpDelete("KnowledgeBase/{id}")]
    public async Task<Result> DeleteAsync(long id)
    {
        await _knowledgeBaseService.DeleteAsync(id);
        return await Result.SucceedAsync();
    }

    /// <summary>
    /// 创建对话
    /// </summary>
    /// <param name="title">对话标题</param>
    /// <returns></returns>
    [HttpPost("Create/Dialogue")]
    public async Task<Result> CreateDialogueAsync(string title)
    {
        await _dialogueService.CreateAsync(title);
        return await Result.SucceedAsync();
    }

    /// <summary>
    /// 对话分页查询
    /// </summary>
    /// <param name="filter">分页输入模型</param>
    /// <returns></returns>
    [HttpGet("Dialogue/Page")]
    public async Task<PageResult<DialogueOutputModel>> PaginationDialogueAsync([FromQuery] DialoguePaginationFilter filter)
    {
        return await _dialogueService.PageAsync(filter);
    }

    /// <summary>
    /// 删除对话
    /// </summary>
    /// <returns></returns>
    [HttpDelete("Dialogue/{id}")]
    public async Task<Result> DeleteDialogueAsync(long id)
    {
        await _dialogueService.DeleteAsync(id);
        return await Result.SucceedAsync();
    }

    /// <summary>
    /// 提问问题
    /// </summary>
    /// <param name="model">输入模型</param>
    /// <returns></returns>
    [HttpPost("Create/Question")]
    public async Task<Result> CreateQuestionAsync(MedicalQuestionAnswerPairsCreateInputModel model)
    {
        await _medicalAssistantService.CreateAsync(model);
        return await Result.SucceedAsync();
    }

    /// <summary>
    /// 提问记录
    /// </summary>
    /// <param name="filter">分页输入模型</param>
    /// <returns></returns>
    [HttpGet("Question/History")]
    public async Task<PageResult<MedicalQuestionAnswerPairsOutputModel>> QuestionHistoryAsync([FromQuery] MedicalQuestionAnswerPairsPaginationFilter filter)
    {
        return await _medicalAssistantService.PageAsync(filter);
         
    }
}
